package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.AddressBook;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Api(tags = "套餐管理相关接口")
@RequestMapping("/admin/setmeal")
public class SetmealController {   //TODO：新增套餐 （500）

    @Autowired
    private SetmealService setmealService;

    /**
     * @Description : 添加套餐
     *  * @param setmealDTO
     * @Return : com.sky.result.Result
     */
    @PostMapping
    @ApiOperation("添加套餐")
    @CachePut(cacheNames = "setmealCache", key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO){

        log.info("添加套餐");
        setmealService.saveWithDishes(setmealDTO);
        return Result.success();
    }

    /**
     * @Description : 分页显示
     *  * @param setmealPageQueryDTO
     * @Return : com.sky.result.Result<com.sky.result.PageResult>
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页显示")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("分页显示");
        PageResult pageResult = setmealService.page(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * @Description : 删除套餐
     *  * @param ids
     * @Return : com.sky.result.Result
     */
    @DeleteMapping
    @ApiOperation(value = "删除套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        log.info("套餐删除");
        setmealService.delete(ids);
        return Result.success();
    }

    /**
     * @Description : 根据id获取套餐
     *  * @param id
     * @Return : com.sky.result.Result<com.sky.vo.SetmealVO>
     */
    @GetMapping("{id}")
    @ApiOperation(value = "根据id获取套餐")
    public Result<SetmealVO> getById(@PathVariable Long id){

        log.info("根据id获取套餐");
        SetmealVO setmealVO=setmealService.getByIdWithDishes(id);
        return Result.success(setmealVO);
    }

    /**
     * @Description : 修改套餐
     *  * @param setmealDTO
     * @Return : com.sky.result.Result
     */
    @PutMapping
    @ApiOperation(value = "修改套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐");
        setmealService.updateWithDishes(setmealDTO);
        return Result.success();
    }

    /**
     * @Description : 套餐起售停售
     *  * @param status
     * @Return : com.sky.result.Result
     */
    @PostMapping("/status/{status}")
    @ApiOperation(value = "套餐起售停售")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result updateStatus(@PathVariable Integer status,Long id){
        log.info("套餐起售停售");
        setmealService.updateStatus(status,id);
        return Result.success();
    }

}
